/*************************************************************************
*
*	simulate_watertemp.c
*
*  Copyright 2007,2008,2009 Steve Stancliff
*
*  Part of the TAOSF SSA system.
*
*
*  This module provides functions for simulating water temperature
*
*
**************************************************************************
*
*  version 1.0.4, 4/24/09, SBS
*   - documentation
*
*  version 1.0.3, 10/4/08, SBS
*   - documentation
*
*  version 1.0.2, 8/27/08, SBS
*   - use status_SetTimeString()
*
*  version 1.0.1, 5/30/08, SBS
*    - move time format into status.h
*
*  version 1.0, 5/17/08, SBS
*    - split out from simulate_science.c
*
*
**************************************************************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but 
*  WITHOUT ANY WARRANTY; without even the implied warranty of 
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License 
*  along with this program; if not, write to the Free Software Foundation, 
*  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
**************************************************************************/
/**
*  @file 
*  @ingroup plat_sci_simulate
*  @brief
*  This module provides functions for simulating water temperature
*  <hr>
**/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#include "waypoints.h"
#include "platform_types.h"
#include "platform_modes.h"
#include "status.h"
//#include "logfile.h"

#include "simulate_watertemp.h"

#define MINTEMP 288.
#define MAXTEMP 298.
#define TEMPINC 0.5


/**
***************************************************************************
*
* simulate_WaterTemp()
*/
/** 
*
* @brief
*
*
**************************************************************************/
SCI_DBL simulate_WaterTemp(
		POSITION p
		, time_t t)
{
	struct tm *timeinfo;

	static SCI_DBL sd;	
	static double inc = TEMPINC;
	static time_t oldtime = 0;

	if(oldtime == 0){

		sd = status_InitSciDbl();
		sd.dValue = MINTEMP;	

	}else{
	
		sd.dValue += inc * (t - oldtime);
	
		if (sd.dValue > MAXTEMP) {
		
			inc = -inc;
			sd.dValue = MAXTEMP;
		
		}else if (sd.dValue < MINTEMP) {
		
			inc = -inc;
			sd.dValue = MINTEMP;
		}
	}
	
	strncpy(sd.sUnits,"K", UNITSTRLEN);

	sd.position = p;
	
	timeinfo = localtime(&t);
//	strftime(sd.sTime, sizeof(sd.sTime), TIMEFORMAT, timeinfo);

	status_SetTimeString(sd.sTime, sizeof(sd.sTime),timeinfo);

	oldtime = t;

	return(sd);
}

